﻿// 版权所有 (c) 2024-2025 yanpeng  联系电话/微信：15572794778  QQ：2357110159
//
// 特此免费授予获得本软件的任何人以处理本软件的权利，但须遵守以下条件：在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。
//
// 软件按“原样”提供，不提供任何形式的明示或暗示的保证，包括但不限于对适销性、适用性和非侵权的保证。
// 在任何情况下，作者或版权持有人均不对任何索赔、损害或其他责任负责，无论是因合同、侵权或其他方式引起的，与软件或其使用或其他交易有关。

namespace Admin.NET.Core;

/// <summary>
/// 系统工序表服务
/// </summary>
[ApiDescriptionSettings(Order = 490)]
public class SysOperationService : IDynamicApiController, ITransient
{
    private readonly ISqlSugarClient _db;
    private readonly SqlSugarRepository<SysOperation> _rep;
    public SysOperationService(SqlSugarRepository<SysOperation> rep, ISqlSugarClient db)
    {
        _rep = rep;
        _db = db;
    }

    /// <summary>
    /// 分页查询工序表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<SysOperationOutput>> Page(SysOperationInput input)
    #region MyRegion
    {
        return await _rep.AsQueryable().Where(u => !u.IsDelete)
            .WhereIF(!string.IsNullOrEmpty(input.OperationName), u => u.OperationName.Contains(input.OperationName))
            .WhereIF(input.OperationMoney != 0, u => u.OperationMoney == input.OperationMoney)
            .WhereIF(input.StyleId!=0,u=>u.StyleId==input.StyleId)
            .Includes(u => u.StyleInfo)
            .Select(u => new SysOperationOutput()
            {
                StyleInfo=u.StyleInfo,
            }, true)
            .ToPagedListAsync(input.Page, input.PageSize);
    } 
    #endregion

    /// <summary>
    /// 增加工序表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task Add(AddSysOperationInput input)
    #region MyRegion
    {
        var entity = input.Adapt<SysOperation>();
        await _rep.InsertAsync(entity);
    }
    #endregion

    /// <summary>
    /// 删除工序表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeleteSysOperationInput input)
    #region MyRegion
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.FakeDeleteAsync(entity);   //假删除
        //await _rep.DeleteAsync(entity);   //真删除
    }
    #endregion

    /// <summary>
    /// 更新工序表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdateSysOperationInput input)
    #region MyRegion
    {
        var entity = input.Adapt<SysOperation>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true, ignoreAllDefaultValue: true).ExecuteCommandAsync();
    } 
    #endregion

    /// <summary>
    /// 获取工序表详情
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<SysOperationOutput> Detail([FromQuery] QueryByIdSysOperationInput input)
    #region MyRegion
    {
        return await _rep.AsQueryable().Where(u => !u.IsDelete && u.Id == input.Id)
            .Includes(u => u.StyleInfo)
            .Select(u => new SysOperationOutput() 
            {
                StyleInfo=u.StyleInfo
            }, true)
            .FirstAsync();
    }
    #endregion

    /// <summary>
    /// 获取工序表列表
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<SysOperationOutput>> List()
    #region MyRegion
    {
        return await _rep.AsQueryable().Where(u => !u.IsDelete)
            .Includes(u=>u.StyleInfo)
            .Select(u=>new SysOperationOutput() 
            {
                StyleInfo=u.StyleInfo,
            },true)
            .ToListAsync();
    } 
    #endregion

}
